<template>
  <div class="address-box page" ref="webview">
    <div class="nav-bar">
      <van-nav-bar title="收货地址" class="bg_white" left-arrow fixed @click-left="$router.go(-1)" />
    </div>
    <div class="content" v-if="dataList.length>0">
      <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
        <van-list v-model="loading" :finished="finished" :finished-text="dataList.length>7?'没有更多了':''" @load="onLoad" :offset="50" :immediate-check="false" v-if="dataList.length>0">
          <template v-for="(item,index) in dataList">
            <div class="list van-hairline--top" :key="index" @click="selectedList(item)">
              <div class="is_default" v-if="item.is_default">
                <span>默认</span>
              </div>
              <div class="info">
                <div class="name">{{item.name}} {{item.mobile | phoneFilter}}</div>
                <p class="address-info">{{item.prov_name}}{{item.city_name}}{{item.area_name}} {{item.address}}</p>
              </div>
              <div class="edit" v-if="$route.query.type !== 'order'" @click.stop="goDetails(item)">
                <span class="icon-address-edit"></span>
              </div>
              <div class="del" v-if="$route.query.type !== 'order'" @click.stop="addressDelete(item.id)">
                <span class="icon-address-del"></span>
              </div>
            </div>
          </template>
        </van-list>
      </van-pull-refresh>
    </div>
    <div class="address-add" @click="goAdd">新增收货地址</div>
  </div>
</template>
<script>
import mixins from '@/utils/mixins';
export default {
  name: 'addressList',
  data () {
    return {
      paramsUrl: 'list',
      apiType: 'user',
      apiUrl: 'address'
    };
  },
  mixins: [mixins],
  created () {
    this.getList('address', true);
  },
  mounted () {

  },
  methods: {
    addressDelete (id) {
      this.$dialog.confirm({
        title: '温馨提醒',
        message: '你确定要删除该地址吗',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        this.$API.user.address({ id: id }, 'delete').then(res => {
          this.$toast('删除成功');
          this.getList('address', true);
        });
      }).catch(() => {
        // on cancel
      });
    },
    goDetails (item) {
      if (this.$route.query.type !== 'order') {
        this.$router.push({ 'path': '/address/sub/edit', query: { id: item.id } });
      } else {
        sessionStorage.setItem('address', JSON.stringify(item));
        this.$router.go(-1);
      }
    },
    selectedList (item) {
      if (this.$route.query.type === 'order') {
        sessionStorage.setItem('address', JSON.stringify(item));
        this.$router.go(-1);
      }
    },
    goAdd () {
      this.$router.push({ 'path': '/address/sub/add' });
    }
  }
};
</script>
<style lang="less" scoped>
.address-box {
  display: flex;
  flex-direction: column;
  .content {
    flex: 1;
    margin-top: 2px;
    box-sizing: border-box;
    overflow: auto;
    .list {
      display: flex;
      padding: 33px 0 33px 40px;
      background-color: #fff;
    }
    .is_default {
      display: flex;
      justify-content: center;
      align-items: center;
      text-align: center;
      width: 80px;
      margin: 0 20px 0 -20px;
      color: #fff;
      span {
        border: 1px solid #007aff; /*no*/
        color: #007aff;
        border-radius: 4px;
        padding: 2px 10px;
      }
    }
    .info {
      flex: 1;
      .name {
        color: #000;
        font-size: 28px;
      }
      .address-info {
        font-size: 24px;
        padding-top: 20px;
        color: #666;
      }
    }
    .edit,
    .del {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .edit {
      width: 40px;
      .icon-address-edit {
        display: block;
        width: 38px;
        height: 38px;
        background-image: url('/static/img/icon-address-edit.png');
        background-position: center center;
        background-repeat: no-repeat;
        background-size: contain;
      }
    }
    .del {
      width: 80px;
      .icon-address-del {
        display: block;
        width: 40px;
        height: 39px;
        background-image: url('/static/img/icon-address-del.png');
        background-position: center center;
        background-repeat: no-repeat;
        background-size: contain;
      }
    }
  }
  .address-add {
    color: #007aff;
    font-size: 34px;
    text-align: center;
    height: 100px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 100px;
    background-color: #fff;
    margin: 40px 50px calc(40px + env(safe-area-inset-bottom));
  }
}
</style>
